/**
   @file resource_model.h
   @brief Prototypes and types that provide access to the modes interface and building resource models.
 */

#ifndef WG_RESOURCE_MODEL_HEADER
#define WG_RESOURCE_MODEL_HEADER

#include "cJSON.h"
#include "random.h"

/** A structure that provides all the information needed for a workload to be generated for a resource. */
typedef struct resource_model resource_model;

/** A mode of operation for a resource's workload. */
typedef struct mode mode;

/** A linked list of mode definitions. These lists make up a hash table for storing modes.  */
typedef struct mode_definition_list mode_definition_list;

struct resource_model {
  void (*birth)(void *); /**< Spawns the first job and schedules the next arrival.  Starts the generator for a resource.*/
  void *state;  /**< Provided for generators to pass a state variable to events if it is convenient. */
  mode **modes;   /**< A pointer to an array of modes that make up the functionality for a resource.*/
  int cnt_modes;   /**< The number of modes stored in the modes array. Used internally to keep track of the size of modes.*/
};

struct mode {
  void *(*function)(void *); /**< Function which implements the mode.*/
  char *key; /**< A key to identify the mode. */
  double probability; /**< The probability of a mode being selected for a given job. */
};

struct mode_definition_list{
  mode_definition_list *next;   
  char *key; /** Identifies the the mode definition. */
  mode *(*build_mode)(cJSON *); /** Translate a mode from a JSON object. */
};

/** Pick a mode from a resource model at random. */
mode * select_mode(resource_model *);

/** Add support for a mode for a given resource. */
void install_mode(mode_definition_list *[HASHSIZE], char *, mode *(*)(cJSON *));

/** Setup modes from a JSON object. To be called after all modes have been installed by install_mode.*/
mode **initialize_modes(mode_definition_list *[HASHSIZE], cJSON *);
/** Count the number of modes inside a JSON object. */
int count_modes(cJSON *);

#endif
